package com.example.lib_storage.database

import android.content.Context
import androidx.room.Room
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import net.sqlcipher.database.SupportFactory

/**
 * @author yaotianxue
 * @description
 * @date 2024/10/23
 */
object DBUtils {//DBUtils单例类，里面都是静态的
    lateinit var appDataBase: AppDataBase
    //新增加表
    val MIGRATION_1_2=object : Migration(1,2){
        override fun migrate(database: SupportSQLiteDatabase) {
            database.execSQL("create table b_takevideo(id INTEGER PRIMARY KEY NOT NULL,videoLocalPath TEXT NOT NULL,userid INTEGER NOT NULL)")
        }

    }

    fun init(context: Context) {
        appDataBase = Room.databaseBuilder(context, AppDataBase::class.java, "ivideo.db")
            .allowMainThreadQueries() //允许在主线程执行数据库操作
            .addMigrations(MIGRATION_1_2) //版本升级
            .addMigrations()
            .openHelperFactory(SupportFactory("123456".toByteArray()))//设置db文件密码
            .build()
    }

    fun getDB():AppDataBase{
        if(appDataBase == null){
            throw IllegalStateException("Database not initialized")
        }
        return appDataBase
    }








}